package cn.jsj.finance.bank;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 添加工资记录处理类
 * @author LI
 *
 */
 public class AddWageInfo extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
	 public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 调试信息
		 cn.jsj.utils.SysLogger.PrintInfo("添加工资记录");

		 //获取参数
		 String wageInfoTime=request.getParameter("wageInfoTime").trim();
		 String lawyerAsName=request.getParameter("lawyerAsName").trim();
		 String wageIncome=request.getParameter("wageIncome").trim();
		 String wagePayout=request.getParameter("wagePayout").trim();
		 String enterWagePerson=request.getParameter("enterWagePerson").trim();
		 String wageBrief=request.getParameter("wageBrief").trim();

		//输出流
		  java.io.PrintWriter out=response.getWriter();
		 //签名信息
		 String lawyerAssSign=request.getParameter("lawyerAssSign").trim();
		 if(lawyerAssSign==""||lawyerAssSign==null||lawyerAssSign.length()==0){
			 cn.jsj.utils.SysLogger.PrintInfo("没有签名");
		 }else{
			 //验证签名是否有效
			 cn.jsj.dao.TblLawyAssistantDAO tlad=new cn.jsj.dao.TblLawyAssistantDAO();
			 int validate=tlad.signValidate(Integer.parseInt(lawyerAsName), lawyerAssSign);
			 //如果签名无效
			 if(validate==0){
				 out.println("{message:'签名无效！'}");
				 return;
			 }
		 }
		 //判断是否为领工资
		 if(Integer.parseInt(wageIncome)==0){
			 //判断是否有足够的钱发工资
			 int wage=Integer.parseInt(wagePayout);
			 //获取工资余额
			 cn.jsj.dao.TblWageManageDAO twmd=new cn.jsj.dao.TblWageManageDAO();
			 int wageLeft=twmd.getWageLeft(Integer.parseInt(lawyerAsName));
			 //如果工资不足
			 if(wageLeft<wage){
				 out.println("{message:'余额不足！'}");
				 return;
			 }
		 }
		 //调试信息
		 cn.jsj.utils.SysLogger.PrintInfo("----->"+lawyerAsName+" "+wageIncome+"  "+wagePayout+"  "+enterWagePerson+"  "+wageBrief);
		 //新建工资对象
		 cn.jsj.pojo.TblWageManage twm=new cn.jsj.pojo.TblWageManage();
		 //赋值
		 twm.setWageTime(cn.jsj.utils.TimeDispose.transferDate(wageInfoTime));
		 twm.setLawyAssistantId(Integer.parseInt(lawyerAsName));
		 twm.setEnterWagePer(enterWagePerson);
		 twm.setWageBrief(wageBrief);
		 twm.setWageIncome(Integer.parseInt(wageIncome));
		 twm.setWagePayout(Integer.parseInt(wagePayout));

		 //保存
		 cn.jsj.dao.TblWageManageDAO twmd=new cn.jsj.dao.TblWageManageDAO();
		 int saveResult=twmd.saveWageInfo(twm);

		 //调试信息
		 cn.jsj.utils.SysLogger.PrintInfo("保存结果------->"+saveResult);


		 //如果保存成功
		 if(saveResult==1){
			 out.println("{message:'保存成功！'}");
		 }else{
			 out.println("{message:'保存失败！'}");
		 }
		 out.flush();
		 out.close();
	}
}